import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Date;


public class ReverseClient {

	public static void main(String[] pArgs) 
	{
		if(pArgs.length<3)
		{
			System.out.println("usage: java Client host port boardnum");
			return;
		}
		try
		{
			Socket lSocket=new Socket(pArgs[0],Integer.parseInt(pArgs[1]));
			PrintWriter lOut=new PrintWriter(lSocket.getOutputStream());
			BufferedReader lIn=new BufferedReader(new InputStreamReader(lSocket.getInputStream()));
	
            lOut.println(pArgs[2]);
            lOut.flush();

            String lLine=lIn.readLine();
            
            
            //create a list of the string
            ArrayList<String> level = new ArrayList<String>();

            //read number of rows
            int lNumRows=Integer.parseInt(lLine);

            //read each row
            for(int i=0;i<lNumRows;i++)
            {
                lLine=lIn.readLine();
                level.add(lLine);
                //here, we would store the row somewhere, to build our board
                //in this demo, we just print it
//               //System.out.println(lLine);
            }
            
            //read number of columns
            int lNumColumns=level.get(0).length();
            
            //build the board 
            Board board = new Board (lNumRows,lNumColumns, level);
            
            
            // create playerNBoxes positions
            PlayerNBoxesReverse playerNBoxes = new PlayerNBoxesReverse(lNumRows,lNumColumns, level);
//            playerNBoxes.setRank(board);
            
            //starting the time .. tic toc
            long start; 
            start=new Date().getTime();
            
            byte[][] jointedBoard = Helper.joinBoards(board,playerNBoxes);
    		System.out.println("The original board ");
    		System.out.println(Board.toString(jointedBoard, jointedBoard.length, jointedBoard[0].length));
    		
    		
            ArrayList<Move> movesToDo = new ArrayList<Move>();
            Retrace rtr = new Retrace(board,playerNBoxes);
            movesToDo = rtr.retraceInterface();
          
            
          
    		
            String solution = "";
            
            //***********************************//
            //**********swapping movesToDo******//
            //*********************************//
            solution = Assist.manWay(board, playerNBoxes, movesToDo);
            System.out.println("solution: "+solution);
 
            ////System.out.println(rankArray);
//            //System.out.println(playerNBoxes);
            
            
            
            
            
            FileOutputStream f = new FileOutputStream("out.txt");
            for (int i=0; i < solution.length(); i++){
                f.write((byte)solution.charAt(i));
              }
            f.flush();
            
            lSocket=new Socket(pArgs[0],Integer.parseInt(pArgs[1]));
			lOut=new PrintWriter(lSocket.getOutputStream());
			lIn=new BufferedReader(new InputStreamReader(lSocket.getInputStream()));
            lOut.println(pArgs[2]);
            lOut.flush();
            for(int i=0;i<lNumRows+1;i++)
            {
                lLine=lIn.readLine();
            }
            
            
            lOut.println(solution);
            long end= ((new  Date().getTime())-start);
            System.out.println("in: "+end+"ms = "+end/1000+"s = "+end/1000/60.+"min = "+end/1000/60/60.0+"h");

            lOut.flush();
            
    
            //read answer from the server
            lLine=lIn.readLine();
    
            System.out.println("!! "+lLine);
            System.out.println("END");

		}
		catch(Throwable t)
		{
			t.printStackTrace();
		}
		
			
		
	}
	
}


